package org.phalanx.client.mapeditor;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.phalanx.client.UI.PhalanxIconButton;
import org.phalanx.client.UI.swing.PhalanxButtonFactory;
import org.phalanx.client.UI.swing.listeners.pButtonListener;
import org.phalanx.client.UI.swing.listeners.pButtonPressed;

public class PhalanxMapTileSetChooserPanel extends JPanel implements ChangeListener, pButtonListener {
	private static final long serialVersionUID = -3644812199373091042L;
	private JRadioButton defaultTileSet;
	private JRadioButton customTileSet;
	private ButtonGroup buttonGroup;
	private JTextField tileSetPath;
	private PhalanxIconButton openFile;
	
	public PhalanxMapTileSetChooserPanel() {
		initializePanel();
		setUpComponents();
		setInitialConditions();
		createChooserGrid();
	}
	
	private void initializePanel() {
		this.setBackground(Color.black);
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
	}
	
	private void setUpComponents() {
		defaultTileSet = new JRadioButton();
		defaultTileSet.setForeground(Color.white);
		defaultTileSet.setOpaque(false);
		defaultTileSet.setText("Default Tile Set");
		
		customTileSet = new JRadioButton();
		customTileSet.setForeground(Color.white);
		customTileSet.setOpaque(false);
		customTileSet.setText("Custom Tile Set");
		
		buttonGroup = new ButtonGroup();
		buttonGroup.add(defaultTileSet);
		buttonGroup.add(customTileSet);
		
		openFile = PhalanxButtonFactory.getButton(PhalanxButtonFactory.DEFAULT_OPEN_FILE_BUTTON);
		
		openFile.addButtonListener(this);
		
		tileSetPath = new JTextField();
		tileSetPath.setColumns(20);
		
		defaultTileSet.addChangeListener(this);
		customTileSet.addChangeListener(this);
	}
	
	public boolean getIsDefaultSelected() {
		return defaultTileSet.isSelected();
	}
	
	public JTextField getFilePath() {
		return tileSetPath;
	}
	
	private void setInitialConditions() {
		defaultTileSet.setSelected(true);
		tileSetPath.setEnabled(false);
	}
	
	private void createChooserGrid() {		
		JPanel chooserPanel = new JPanel();
		chooserPanel.setBackground(Color.black);
		chooserPanel.setLayout(new GridBagLayout());
		
		GridBagConstraints c = new GridBagConstraints();
		
		c.gridx = 0;
		c.gridy = 0;
		c.fill = GridBagConstraints.BOTH;
		
		chooserPanel.add(defaultTileSet, c);
		
		c = new GridBagConstraints();
		
		c.gridx = 0;
		c.gridy = 1;
		c.fill = GridBagConstraints.BOTH;
		
		chooserPanel.add(customTileSet, c);
		
		c = new GridBagConstraints();
		
		c.gridx = 0;
		c.gridy = 2;
		c.fill = GridBagConstraints.BOTH;
		
		Container cont2 = new Container();
		cont2.setBackground(Color.black);
		cont2.setLayout(new FlowLayout(FlowLayout.LEFT));
		JLabel filePathLabel = new JLabel();
		filePathLabel.setForeground(Color.white);
		filePathLabel.setText("Tile Set Path");
		
		cont2.add(filePathLabel);
		cont2.add(tileSetPath);
		
		Container cont3 = new Container();
		cont3.setBackground(Color.black);
		cont3.setLayout(new FlowLayout(FlowLayout.RIGHT));
		cont3.add(openFile);
		
		cont2.add(cont3);
		
		chooserPanel.add(cont2, c);
		
		this.add(chooserPanel);
	}

	public void stateChanged(ChangeEvent c) {
		setFilePathEnabled(!this.getIsDefaultSelected());
	}
	
	public void setFilePathEnabled(boolean state) {
		tileSetPath.setEnabled(state);
		openFile.setEnabled(state);
		tileSetPath.setBackground(state ? Color.white : Color.gray);
	}

	public void actionPerformed(pButtonPressed p) {
		if (p.getComponent().equals(openFile)) {
			openFile();
		}
		
	}
	
	private void openFile() {
		JFileChooser fc = new JFileChooser();
		int fileOption = fc.showOpenDialog(this);

		if (fileOption == JFileChooser.APPROVE_OPTION){
			tileSetPath.setText(fc.getSelectedFile().getAbsolutePath());
		}
	}
}
